import 'package:library_base/entities/lazy_page_entity.dart';
import 'package:library_base/entities/purchase_order_entity.dart';
import 'package:library_base/entities/task_detail_entity.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:get/get.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';

class PurchasePaymentController
    extends BaseListController<LazyPageEntity<PurchaseOrderEntity>, PurchaseOrderEntity> {

  Rx<String> payStatus = ''.obs;

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.PURCHASER_ORDERS,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "payStatus": payStatus.value,
        });
    return super.requestData();
  }

  void tryReLoad() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.PURCHASER_ORDERS,
        isRefresh: false,
        method: FBResultFul.GET,
        params: {},
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "payStatus": payStatus.value,
        });
  }

  @override
  List<PurchaseOrderEntity> transformObject(
      LazyPageEntity<PurchaseOrderEntity> data, bool isRefresh) {
    return data.list;
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.PURCHASER_ORDERS,
        isRefresh: true,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(true)}',
        });
  }

  @override
  Future onLoadCallback() async {
    requestGeneralListData(Apis.PURCHASER_ORDERS,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "payStatus": payStatus.value,
        });
  }

  ///付款申请
  onPaymentApplication(PurchaseOrderEntity purchaseOrderEntity) {
    var map = {};
    map['Purchase_Id'] = purchaseOrderEntity.purchaseId;
    map['CallBack'] = (){
      tryReLoad();
    };
    DialogUtil.showMultipleImageSelectionDialog(arguments: map);
  }

  ///订单修正
  onCorrectionOrder(PurchaseOrderEntity purchaseOrderEntity) {
    requestUniversal<TaskDetailEntity>(Apis.MODIFY_ORDER,
        method: FBResultFul.DELETE,
        queryParameters: {"id": purchaseOrderEntity.purchaseId},
        params: {"id": purchaseOrderEntity.purchaseId},
        isShowToastTip: false,
        resultSuccessCall: (data) {
          Get.back();
          Get.toNamed(
              Routers.createPurchaseOrderPage,
              arguments: {
                'TaskDetailEntity': data,
                'isShowGenerateSalesOrderDialog': true,
              }
          );
        });
  }

}
